package com.wangmei.controller;

import com.wangmei.model.Attribute;
import com.wangmei.model.Attributevalue;
import com.wangmei.model.Productkuid;
import com.wangmei.pojo.*;
import com.wangmei.service.AttributeService;
import com.wangmei.service.ProductkuidService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.Attr;
import unitTool.JsonRsp;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/attribute")
public class AttributeController {
    private final Logger logger = LoggerFactory.getLogger(Attribute.class);
    @Autowired
    private AttributeService attributeService;
    @Autowired
    private ProductkuidService productkuidService;

    /**
     * 通过cid获得某个商品的属性
     */
    @RequestMapping(value = "/getAttributeByCid",method = RequestMethod.GET)
    public JsonRsp getAttributeByCid(@RequestParam(value = "cid", required = false)Integer cid,
                                     @RequestParam(value = "pid", required = false)Integer pid){
        Map<String,Object> map = new HashMap<>();
        if(pid != null)
            map.put("pid",pid);
        if(cid != null)
            map.put("cid",cid);
        List<AttributeViewPojo> aList = attributeService.getAttributeByCid(map);
//        AttributeAndPidPojo ap = new AttributeAndPidPojo();
//        ap.setPid(pid);
//        ap.setAttributeList(aList);

        return JsonRsp.success(createTreeList(aList));
    }

    private List<AttributeViewPojoV2> createTreeList(List<AttributeViewPojo> aList) {
        int pid = 0;
        int a = -1;
        int tid = 1;
        int len = aList.size();
        List<AttributeViewPojoV2> relist = new ArrayList<>();
        AttributeViewPojoV2 avp;
        AttributeViewPojo data;
        for(int i= 0;i<len;i++) {
            data = aList.get(i);
            avp = new AttributeViewPojoV2();
            data.setTid(i + 1);
            if(i == 0 || a != data.getAid()){
                a = data.getAid();
                pid = tid;
                avp.setTid(tid++);
                avp.setAid(data.getAid());
                avp.setName(data.getAname());
                avp.setPid(0);
                relist.add(avp);
                avp = new AttributeViewPojoV2();
            }
            avp.setTid(tid++);
            avp.setAid(data.getAid());
            avp.setPid(pid);
            avp.setKUID(data.getKUID());
            avp.setStorenum(data.getStorenum());
            avp.setName(data.getVname());
            relist.add(avp);

        }


        return relist;
    }

    /**
     *添加商品属性的属性值(如果存在该属性值，就不添加),同时插入一条库存的记录,库存为0
     */
    @RequestMapping(value = "/addAttributevalue",method = RequestMethod.POST)
    public JsonRsp addAttributevalue(Attributevalue attributevalue, @RequestParam("pid")int pid) throws UnsupportedEncodingException {
        String vname = attributevalue.getVname();
        Attributevalue a = attributeService.getAttributevalueByVname(vname);
        logger.info(attributevalue.getVname());
        if (a != null){
            return JsonRsp.fail(400,"已有该属性值,不需要再添加");
        }else{
            int i = attributeService.addAttributevalue(attributevalue);
            if (i>0){
                Integer vid = attributeService.getAttributevalueByVname(vname).getVid();
                Productkuid productkuid = new Productkuid();
                productkuid.setPid(pid);
                productkuid.setStorenum(0);
                productkuid.setVid(vid);
                productkuidService.addProductkuid(productkuid);
                return JsonRsp.success("","添加成功");
            }
            return JsonRsp.fail(400,"添加失败");
        }
    }

    /**
     * 根据cid查询该类型的全部属性，且可以查到该属性下的属性值
     * @param cid
     * @return
     */
    @RequestMapping(value = "/getAttributeAndValue",method = RequestMethod.GET)
    public JsonRsp getAttributeAndValue(@RequestParam("cid")int cid){
        List<AttributeAndValuePojo> aavpList = new ArrayList<>();
        List<Attribute> attributeList = attributeService.getAttributeList(cid);
        for (Attribute a : attributeList){
            AttributeAndValuePojo aavp = new AttributeAndValuePojo();
            aavp.setAname(a.getAname());
            int aid = a.getAid();
            List<Attributevalue> attvalueList = attributeService.getAttvalueByAid(aid);
            List<String> list = new ArrayList<>();
            for (Attributevalue av : attvalueList){
                list.add(av.getVname());
            }
            aavp.setVnameList(list);
            aavpList.add(aavp);
        }
        return JsonRsp.success(aavpList);
    }
}
